$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'npp/list',
        datatype: "json",
        colModel: [			
			{ label: 'nppId', name: 'nppId', index: 'npp_id', width: 50, key: true },
			{ label: '嘎查名称', name: 'villageName', index: 'village_name', width: 80 }, 			
			{ label: '嘎查面积', name: 'villageArea', index: 'village_area', width: 80 }, 			
			{ label: '年份', name: 'year', index: 'year', width: 80 }, 			
			{ label: '年均温度', name: 'temperature', index: 'temperature', width: 80 }, 			
			{ label: 'npp', name: 'npp', index: 'npp', width: 80 }, 			
			{ label: '年均降水', name: 'rainfall', index: 'rainfall', width: 80 }, 			
			{ label: '日照天数', name: 'windyDays', index: 'windy_days', width: 80 }, 			
			{ label: '光照天数', name: 'sunshineDays', index: 'sunshine_days', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		year:'',
        village_name:'',
		npp: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		yearquery: function (event) {
        			//input.focus();
        			//alert(vm.starttime);
        			 $("#jqGrid").jqGrid('setGridParam',{
        				 url:baseURL + 'npp/yearlist',
        				 postData:{
        					 'year':vm.year,
        					 'village_name':vm.village_name
        				 },
        				 datattype:'json',
        				 mtyp:'GET',
        				 viewrecords: true,
        			     height: 385,
        			     rowNum: 10,
        			     rowList : [10,30,50],
        			     rownumbers: true,
        			     rownumWidth: 25,
        			     autowidth:true,
        			     multiselect: true,
        			     pager: "#jqGridPager",
        			     jsonReader : {
        			         root: "page.list",
        			         page: "page.currPage",
        			         total: "page.totalPage",
        			         records: "page.totalCount"
        			     },
        			     prmNames : {
        			         page:"page",
        			         rows:"limit",
        			         order: "order"
        			     },
        			 }).trigger('reloadGrid');


        		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.npp = {};
		},
		update: function (event) {
			var nppId = getSelectedRow();
			if(nppId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(nppId)
		},
		saveOrUpdate: function (event) {
			var url = vm.npp.nppId == null ? "npp/save" : "npp/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.npp),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var nppIds = getSelectedRows();
			if(nppIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "npp/delete",
                    contentType: "application/json",
				    data: JSON.stringify(nppIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(nppId){
			$.get(baseURL + "npp/info/"+nppId, function(r){
                vm.npp = r.npp;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});